﻿using System;
using System.Collections.Generic;
using TSharp.Core.Authentication.Facade;
namespace TSharp.Core.Permissions
{

    

    /// <summary>
    /// 
    /// <para>2010/11/15</para>
    /// 	<para>TANGJINGBO</para>
    /// 	<author>tangjingbo</author>
    /// </summary>
    public abstract class Popedom : IComparable<Popedom>, IEquatable<Popedom>, IComparable
    {
        public readonly static Popedom Full = new Inner() { _item = -1, _value = 2147483645 };
        public readonly static Popedom Empty = new Inner() { Item = -2, _value = 0 };
        private int _item;

        /// <summary>
        /// 权限标识
        /// </summary>
        /// <value>The item.</value>
        public int Item
        {
            set
            {
                if (value < 0 || value > 30)
                    throw new ArgumentOutOfRangeException("Value");
                _item = value;
                _value = 1 << _item;
            }
        }
        /// <summary>
        /// 权限值
        /// </summary>
        /// <value>The value.</value>
        public int Value
        {
            get { return _value; }
        }

        private int _value;

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }

        private class Inner : Popedom
        {
            internal Inner() { }
            public Inner(int item, string title)
            {
                this.Item = item;
                this.Title = title;
            }
        }

        public static implicit operator int(Popedom p)
        {
            return p._item;
        }
        public static implicit operator Popedom(int item)
        {
            return new Inner(item, "");
        }

        public static Popedom operator &(Popedom a, Popedom b)
        {
            return a.Value & b.Value;
        }
        public static Popedom operator |(Popedom a, Popedom b)
        {
            return a.Value | b.Value;
        }

        public static bool operator ==(Popedom a, Popedom b)
        {
            if (a != null && b != null)
                return a._item == b._item;
            return false;
        }
        public static bool operator !=(Popedom a, Popedom b)
        {
            if (a != null && b != null)
                return a._item != b._item;
            return false;
        }
        #region 接口实现

        public int CompareTo(Popedom other)
        {
            if (other == null) return 1;
            return this._item.CompareTo(other._item);
        }

        public int CompareTo(object obj)
        {
            return CompareTo(obj as Popedom);
        }

        public bool Equals(Popedom other)
        {
            if (other == null) return false;
            return this._item.Equals(other._item);
        }
        public override bool Equals(object obj)
        {
            return Equals(obj as Popedom);
        }
        public override int GetHashCode()
        {
            return _item.GetHashCode();
        }

        #endregion
    }
}